package util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import po.Client;
import po.Dish;
import po.Merchant;
import po.OrderItems;
import po.Orders;
import vo.ClientVo;
import vo.DishVo;
import vo.MerchantVo;
import vo.OrderItemsVo;
import vo.OrdersVo;

public class Po2Vo {
	public static MerchantVo merchentPo2Vo(Merchant c) {
		MerchantVo vo = new MerchantVo();
		vo.setmName(c.getmName());
		vo.setAddress(c.getAddress());
		List<Dish> dishList = new ArrayList<Dish>();
		Dish dish = null;
		if(c.getDishs() != null){
			for(Dish d:c.getDishs()){
				dish = new Dish();
				dish.setdId(d.getdId());
				dish.setdName(d.getdName());
				dish.setImg(d.getImg());
				dish.setPrice(d.getPrice());
				dishList.add(dish);
			}
		}
	
		vo.setDishs(dishList);
		vo.setGrade(c.getGrade());
		vo.setLogo(c.getLogo());
		vo.setmId(c.getmId());
		vo.setmTel(c.getmTel());
		vo.setType(c.getType());
		return vo;
	}

	public static ClientVo clientPo2Vo(Client client) {
		ClientVo clientVo = new ClientVo();
		clientVo.setCaddress(client.getCaddress());
		clientVo.setCid(client.getCid());
		clientVo.setCname(client.getCname());
		clientVo.setCtel(client.getCtel());
		return clientVo;
	}

	public static OrderItemsVo orderItemsPo2Vo(OrderItems orderItems) {
		OrderItemsVo orderItemsVo = new OrderItemsVo();
		orderItemsVo.setDid(orderItems.getDid());
		orderItemsVo.setdName(orderItems.getdName());
		orderItemsVo.setImg(orderItems.getImg());
		orderItemsVo.setNum(orderItems.getNum());
		orderItemsVo.setOiid(orderItems.getOiid());
		orderItemsVo.setPrice(orderItems.getPrice());
		orderItemsVo.setTotal(orderItems.getPrice() * orderItems.getNum());
		return orderItemsVo;
	}

	public static OrdersVo ordersPo2Vo(Orders orders) {
		OrdersVo ordersVo = new OrdersVo();
		ordersVo.setComplain(orders.getComplain());
		ordersVo.setoId(orders.getoId());
		ordersVo.setOgrade(orders.getOgrade());
		ordersVo.setClient(Po2Vo.clientPo2Vo(orders.getClient()));
		ordersVo.setMerchant(Po2Vo.merchentPo2Vo(orders.getMerchant()));
		List<OrderItems> orderItems = orders.getOrderItems();
		List<OrderItemsVo> orderItemsVos = new ArrayList<OrderItemsVo>();
		double total = 0;
		for (OrderItems orderItems2 : orderItems) {
			orderItemsVos.add(Po2Vo.orderItemsPo2Vo(orderItems2));
			total += orderItems2.getPrice() * orderItems2.getNum();
		}
		ordersVo.setTotal(total);
		ordersVo.setOrderItems(orderItemsVos);
		ordersVo.setNumOfOrderItems(orderItems.size());
		ordersVo.setStatusInt(orders.getStatus());
		ordersVo.setReceiveInt(orders.getReceive());
		if (orders.getStatus() == 0) {
			ordersVo.setStatus("商家未接单");
		} else if (orders.getStatus() == 1) {
			ordersVo.setStatus("商家已接单");
		} else if (orders.getStatus() == 2) {
			ordersVo.setStatus("商家已拒单");
		}
		if(orders.getStatus()==2){
			ordersVo.setReceive("订单被取消");
		}else if(orders.getReceive()==0){
			ordersVo.setReceive("未确认收货");
		}else if(orders.getReceive()==1){
			ordersVo.setReceive("已确认收货");
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ordersVo.setDt(sdf.format(orders.getDt()));
		if("".equals(orders.getComplain())&&orders.getOgrade()==-1){
			ordersVo.setEvaluate(0);
		}else if(orders.getOgrade()==-1){
			ordersVo.setEvaluate(2);
		}else if("".equals(orders.getComplain())){
			ordersVo.setEvaluate(1);
		}else{
			ordersVo.setEvaluate(3);
		}
		return ordersVo;
	}

	public static DishVo dishPo2Vo(Dish d, int count) {
		DishVo vo = new DishVo();
		vo.setCount(count);
		vo.setdId(d.getdId());
		vo.setdName(d.getdName());
		vo.setImg(d.getImg());
		vo.setPrice(d.getPrice());
		vo.setStatus(d.getStatus());
		return vo;
	}
}
